<?php
class CategoriaController extends Zend_Controller_Action {

	public function indexAction(){
		$categoriaModel = new Application_Model_Categoria();
		
		$this->view->categorias = $categoriaModel->fetchAll()->toArray();
	}
	
	public function formularioAction(){
		$form = new Application_Form_Categoria_Adicionar();
		
		$cd_categoria = $this->_request->getParam('categoria',false);
		if( $cd_categoria ){
			$categoriaModel = new Application_Model_Categoria();
			$values = $categoriaModel->find( array('cd_categoria'=>$cd_categoria) )->toArray();
			
			if( !empty($values) ){
				$values = $values[0];
			}
			
			$form->setDefaults( $values );
		}
		
		if( $this->_request->isPost() ){
			$form->setDefaults( $this->_request->getPost() );
			$categoriaModel = new Application_Model_Categoria();
			$categoriaModel->insert( $form->getValues() );
		}
		$form->setAction( $this->view->url( array('controller'=>'categoria','action'=>'formulario') ) );
		$this->view->form = $form;
	}
	
}